package com.ytrsoft.util;

import com.ytrsoft.entity.Params;

import javax.swing.*;
import java.util.List;

public class GenerateLyrics extends SwingWorker<List<String>, Void> {

    private final Params params;

    public interface Task {
        void onData(List<String> list);
        void onDone();
        void onError(String error);
    }

    private Task task;

    public GenerateLyrics(Params params) {
        this.params = params;
    }

    public void setOnTask(Task task) {
        this.task = task;
    }

    @Override
    protected List<String> doInBackground() throws Exception {
        return Request.generateLyrics(params);
    }

    public void start() {
        execute();
    }

    @Override
    protected void done() {
        try {
            List<String> list = get();
            if (task != null) {
                task.onData(list);
            }
        } catch (Exception e) {
            if (task != null) {
                task.onError(e.getMessage());
            }
        } finally {
            if (task != null) {
                task.onDone();
            }
        }
    }
}
